<?php

/**
 * This is the model class for table "public.workflow".
 */
require_once(dirname(__FILE__) . '/base/WorkflowBase.php');
class Workflow extends WorkflowBase
{
    const TYPE_NORMAL = 0, TYPE_INITITAL = 1, TYPE_FINAL = 2;

    public function behaviors()
    {
        return array(
            'timestamp' => array(
                'class' => 'TimestampBehavior',
            ),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        return array(
            'site' => array(self::BELONGS_TO, 'Site', 'site_id'),
        );
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
            array('name', 'required'),
            array('name', 'uniqueName'),
            array('url', 'required', 'on' => 'insert, update'),
            array('type, creation_datetime, last_update, site_id', 'safe'),
            array('id, site_id, name, url, creation_datetime, last_update, type, locale_id', 'safe', 'on' => 'search'),
        );
    }

    public function uniqueName($attribute, $params)
    {
        $condition = 'name = :name AND site_id = :site_id';
        $params = array(
            ':name' => $this->{$attribute},
            ':site_id' => $this->site_id,

        );
        if ($this->id) {
            $condition .= ' AND id != :id';
            $params[':id'] = $this->id;
        }

        if (self::model()->exists($condition, $params)) {
            $this->addError($attribute, $this->{$attribute} . ' is exist.');
            return false;
        }
        return true;
    }

    public function getSiteOptions()
    {
        $models = FSM::run('Cms.Site.getList')->models;
        if (count($models))
            return CHtml::listData($models, 'id', 'name');
        return array();
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @$operator='IS NULL' : Default locale is null for standard base workflow.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search($operator = 'IS NULL')
    {
        $criteria = new CDbCriteria;

        $criteria->compare('t.name', $this->name, true);
        $criteria->compare('t.url', $this->url, true);
        $criteria->compare('t.site_id', $this->site_id);
        $criteria->compare('t.locale_id', $this->locale_id);
        //$criteria->addCondition('t.locale_id '.$operator);
        $criteria->with = array(
            'site' => array('select' => 'name'),
        );

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
            'sort' => array(
                'defaultOrder' => 't.site_id, t.name'
            ),
            'pagination' => array(
                'pageSize' => Settings::BO_PAGE_SIZE,
            ),
        ));
    }

    public function getTypeOptions()
    {
        return array(
            self::TYPE_NORMAL => 'Normal',
            self::TYPE_INITITAL => 'Initial',
            self::TYPE_FINAL => 'Final',
        );
    }

    public function getTypeText($id = null)
    {
        if (is_null($id))
            $id = $this->type;
        $options = $this->getTypeOptions();
        return isset($options[$id]) ? $options[$id] : '';
    }
}